﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Prod;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.Bpt;
using ESOL.WinUI.Sales;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanOrderQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        private ProdPlanOrderEntity mSelectedProdPlanOrder;
        public ProdPlanOrderEntity SelectedProdPlanOrder { get { return mSelectedProdPlanOrder; } }
        #endregion

        #region ctor
        public frmMaintainProductPlanOrderQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PSTATUS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtProdPlanApplicationCode.Text) == false)
            {
                condition += " AND PA.APPLICATION_CODE LIKE '%" + txtProdPlanApplicationCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND P.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PPO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                condition += " AND PPO.DELIVERY_DATE >= '" + ((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue != null)
            {
                condition += " AND PPO.DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (string.IsNullOrEmpty(txtProdPlanApplicationCode.Text) == false)
            {
                condition += " AND PA.APPLICATION_CODE='"+txtProdPlanApplicationCode.Text+"'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "";
            DataTable dt = ProdPlanOrderBs.GetByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProdPlanOrder = new ProdPlanOrderEntity(dr);
        }

        protected override void Click_Confirm()
        {
            if (mSelectedProdPlanOrder == null)
            {
                MessageBox.Show("请选择生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedProdPlanOrder == null)
            {
                MessageBox.Show("请选择生产计划单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtProdPlanApplicationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanApplication != null)
            {
                txtProdPlanApplicationCode.Tag = frm.SelectedProdPlanApplication;
                txtProdPlanApplicationCode.Text = frm.SelectedProdPlanApplication.ApplicationCode;
            }
            else
            {
                txtProdPlanApplicationCode.Tag = null;
                txtProdPlanApplicationCode.Text = "";
            }
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                txtProductCode.Tag = frm.SelectedProduct;
                txtProductCode.Text = frm.SelectedProduct.ProductCode;
            }
            else
            {
                txtProductCode.Tag = null;
                txtProductCode.Text = "";
            }
        }
        #endregion

    }
}
